package com.briup.es.service.impl;

import com.briup.es.domain.bean.Category;
import com.briup.es.domain.vo.CategoryVO;
import com.briup.es.mapper.CategoryMapper;
import com.briup.es.service.CategoryService;
import com.briup.es.utils.BeanUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {

    private final CategoryMapper categoryMapper;

    @Override
    public List<CategoryVO> queryAll() {
        return categoryMapper.queryAllWithChildren();
    }

    private List<CategoryVO> test(){
        // 先查询所有的一级栏目，一级栏目的parentId为null
        List<Category> categories = categoryMapper.queryByParentId(null);
        List<CategoryVO> categoryVOS = BeanUtil.bean2VOS(categories, CategoryVO.class);
        // 遍历所有的一级，栏目查询每一个一级栏目下的二级栏目
        categoryVOS.forEach(categoryVO -> {
            List<Category> children = categoryMapper.queryByParentId(categoryVO.getId());
            // 将查询到的子栏目数据进行bean转换，同时将数据放入到父栏目中的children中
            categoryVO.setChildren(BeanUtil.bean2VOS(children,CategoryVO.class));
        });
        return categoryVOS;
    }
}
